<!DOCTYPE html>
<html>
<head>
	<title>Bechdel-Wallace Test</title>
	<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
	<script src="lib/brat-linked-vis.js"></script>
	<style>
	div#simple_result { font-size: 72pt; }
	div#simple_result.success { color: green; }
	div#simple_result.failure { color: red; }
	</style>
</head>

<body>
	<h1>Waiting to select graph...</h1>

	<div id="simple_result"></div>

	<div id="table_result" style="width: 100%; height: 250px;"></div>

	<div id="graph-details">
		<label for="document_list">Document</label><select id="document_list" name="document_list"></select>
		<button id="refresh_document_list">Refresh</button>
	</div>

<script>

	sparql_root = 'http://brat.kludge.co.uk/sparql/';
	chart = null;

	$( document ).ready(function() {
		$('#document_list').change(function () {
			refresh_page();
		});

		$('#refresh_document_list').click(function() {
			refresh_document_list(sparql_root);
		});

	    refresh_document_list(sparql_root);
	});


	function refresh_page()
	{
	    update_table();
	}

	function update_table ( )
	{
		$("h1").text("Document table for " + $('#document_list option:selected').text());
		graph_url = $('#document_list').val();
		prepareData(graph_url);
	}

	function prepareData ( graph_url )
	{
	    var query_string = 'PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> \
                            PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> \
                            SELECT DISTINCT ?event ?char1 ?char2 ?label ?name1 ?name2 WHERE {   \
                            	GRAPH <__GRAPH__> { \
                                ?event a <http://contextus.net/ontology/ontomedia/ext/events/social#Conversational> ; \
	                            <http://contextus.net/ontology/ontomedia/core/expression#has-subject-entity> ?char1;  \
	                            <http://contextus.net/ontology/ontomedia/core/expression#has-object-entity> ?char2;  \
	                            rdfs:label ?label . \
                                ?char1 <http://www.w3.org/2002/07/owl#sameAs> ?char1_local1 .  \
                                ?char1_norm1 <http://www.w3.org/2002/07/owl#sameAs> ?char1_local1 ;  \
                                <http://contextus.net/ontology/ontomedia/ext/common/trait#has-trait> [a  <http://contextus.net/ontology/ontomedia/ext/common/trait#Female>] .  \
                                ?char1 <http://www.w3.org/2002/07/owl#sameAs> ?char1_local2 .  \
                                ?char1_norm2 <http://www.w3.org/2002/07/owl#sameAs> ?char1_local2 ;  \
                                <http://contextus.net/ontology/ontomedia/ext/common/trait#has-trait> [ a <http://contextus.net/ontology/ontomedia/ext/common/trait#Name>; <http://contextus.net/ontology/ontomedia/ext/common/trait#has-name> ?name1] .   \
                                ?char2 <http://www.w3.org/2002/07/owl#sameAs> ?char2_local1 .  \
                                ?char2_norm1 <http://www.w3.org/2002/07/owl#sameAs> ?char2_local1 ;  \
                                <http://contextus.net/ontology/ontomedia/ext/common/trait#has-trait> [a <http://contextus.net/ontology/ontomedia/ext/common/trait#Female>] .  \
                                ?char2 <http://www.w3.org/2002/07/owl#sameAs> ?char2_local2 .  \
?char2_norm2 <http://www.w3.org/2002/07/owl#sameAs> ?char2_local2 ;  \
                                <http://contextus.net/ontology/ontomedia/ext/common/trait#has-trait> [ a <http://contextus.net/ontology/ontomedia/ext/common/trait#Name>; <http://contextus.net/ontology/ontomedia/ext/common/trait#has-name> ?name2] ;  \
                                FILTER(?char1 != ?char2) .  \
                                OPTIONAL { ?event <http://contextus.net/ontology/ontomedia/core/expression#refers-to> ?char3 .  \
	                                ?char3 <http://contextus.net/ontology/ontomedia/ext/common/trait#has-trait> [a <http://contextus.net/ontology/ontomedia/ext/common/trait#Male>]. }  \
                                OPTIONAL { ?event <http://contextus.net/ontology/ontomedia/core/expression#refers-to> ?group .  \
	                                ?group <http://contextus.net/ontology/ontomedia/core/expression#contains> ?char4 .  \
	                                ?char4 <http://contextus.net/ontology/ontomedia/ext/common/trait#has-trait> [a <http://contextus.net/ontology/ontomedia/ext/common/trait#Male>]. }  \
                                FILTER( !bound( ?char3 ) )  \
                                FILTER( !bound( ?char4 ) )  \
                                } } ORDER BY ?event';
		$.ajax({
    		url: sparql_root,
    		dataType: 'json',
		    data: {
        		query: query_string.replace('__GRAPH__', graph_url),
		    },

		    success: function( response ) {
		    	if (response.results.bindings.length == 0)
		    	{
		    		$('#simple_result').html("NO").addClass('failure').removeClass('success');
		    		$('#table_result').html('');
		    	}
		    	else
		    	{
		    		$('#simple_result').html("YES").removeClass('failure').addClass('success');
		    		table_html = '<table><tr><th>From</th><th>To</th></tr>'
			    	$.each(response.results.bindings, function (index, triple) {
			    		table_html += "<tr><td>" + triple['name1']['value'] + "</td><td>" + triple['name2']['value'] + "</td></tr>"
			    	});
		    		table_html += '</table>'
		    		$('#table_result').html(table_html);
		    	}
		    }
		});
	}


</script>

</body>
</html>